/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: query.c
> Author: sy
> Website:www.sy.org
> Created Time: 2024年09月23日 星期一 18时00分11秒
***************************************************/

//#include "enroll.c"
#include "query.h"

// 查询
void Query()
{
    struct manager man; // 初始化结构体并置零
    memset(&man, 0, sizeof(man));

    strncpy(man.type, "MANAGER", sizeof(man.type));
    strncpy(man.directives, "Query", sizeof(man.directives));

    struct sockaddr_in addr = {0};
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    // 建立连接
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        return;
    }

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        return;
    }

    char *jsonStr = input_cJSON_query(man);
    if (!jsonStr)
    {
        fprintf(stderr, "Failed to create JSON string\n");
        close(sockfd);
        return;
    }

    // 发送请求并接收响应
    char *response = sendto_and_receive(jsonStr, sockfd);
    free(jsonStr); // 释放jsonStr内存

    if (response)
    {
        output_cJSON_query(response); // 修改输出函数名为output_cJSON_query
        free(response);               // 释放响应内存
    }
    else
    {
        fprintf(stderr, "未收到响应\n");
    }

    close(sockfd); // 在完成所有操作后关闭连接
}

char *input_cJSON_query(struct manager man)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
        return NULL;

    cJSON_AddItemToObject(json, "type", cJSON_CreateString(man.type));
    cJSON_AddItemToObject(json, "directives", cJSON_CreateString(man.directives));

    char *dest = cJSON_Print(json);
    cJSON_Delete(json); // 释放json内存
    return dest;
}

void output_cJSON_query(char *dest)
{
    cJSON *json_response = cJSON_Parse(dest);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        return;
    }

    cJSON *node = cJSON_GetObjectItem(json_response, "directives");
    if (node && node->type == cJSON_String)
    {
        if (strcmp(node->valuestring, "Query") == 0)
        {
            node = cJSON_GetObjectItem(json_response, "info");
            if (node && node->type == cJSON_String)
            {
                printf("设备信息:\n%s\n", node->valuestring);
            }
        }
    }

    cJSON_Delete(json_response); // 释放解析后的JSON对象
}

// 退出
void Quit()
{
    struct manager man;
    memset(&man, 0, sizeof(man));

    strcpy(man.type, "MANAGER");
    strcpy(man.directives, "Quit");

    struct sockaddr_in addr = {0};
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        return;
    }

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        return;
    }

    char *jsonStr = input_cJSON_quit(man);
    if (!jsonStr)
    {
        fprintf(stderr, "Failed to create JSON string\n");
        close(sockfd);
        return;
    }

    // 发送请求并接收响应
    char *response = sendto_and_receive(jsonStr, sockfd);
    free(jsonStr); // 释放jsonStr内存

    if (response)
    {
        output_cJSON_quit(response); // 修改输出函数名为output_cJSON_query
        free(response);              // 释放响应内存
    }
    else
    {
        fprintf(stderr, "未收到响应\n");
    }

    close(sockfd); // 在完成所有操作后关闭连接
}

char *input_cJSON_quit(struct manager man)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
    {
        printf("Failed to create JSON object\n");
        return NULL;
    }

    cJSON_AddItemToObject(json, "type", cJSON_CreateString(man.type));
    cJSON_AddItemToObject(json, "directives", cJSON_CreateString(man.directives));

    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}

void output_cJSON_quit(char *dest)
{
    cJSON *json_response = cJSON_Parse(dest);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        return;
    }

    cJSON *node = cJSON_GetObjectItem(json_response, "directives");
    if (node && node->type == cJSON_String)
    {
        if (strcmp(node->valuestring, "Quit") == 0)
        {
            node = cJSON_GetObjectItem(json_response, "ause");
            if (node && node->type == cJSON_String)
            {
                if (strcmp(node->valuestring, "success") == 0)
                {
                    printf("quit successful\n");
                }
                else if (strcmp(node->valuestring, "fail") == 0)
                {
                    printf("quit failed\n");
                    node = cJSON_GetObjectItem(json_response, "info");
                    if (node && node->type == cJSON_String)
                    {
                        printf("info: %s\n", node->valuestring);
                    }
                }
            }
        }
    }

    cJSON_Delete(json_response);
    free(dest);
}
